# encoding: utf-8
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# author:darkleo
# create Date:2016/12/2
from tkinter import *
from tkinter.font import *
from music import BaiduMusic
from music.PlayUtil import PlayUtil
from gui import event
# 加载歌曲信息
baidu_music = BaiduMusic.BaiduMusic()
music_data = baidu_music.get_music()
paly_util = PlayUtil(baidu_music)
event_obj = event.Event(paly_util)


def get_screen_size(window):
    return window.winfo_screenwidth(), window.winfo_screenheight()


def get_window_size(window):
    return window.winfo_reqwidth(), window.winfo_reqheight()


def center_window(root, width, height):
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    print(size)
    root.geometry(size)


def init_window_size(window):
    center_window(window, 600, 400)
    window.maxsize(600, 400)
    window.minsize(300, 240)


def init_frame():
    frame_top_music_category = Frame(width=400, height=40, bd=1, relief=SUNKEN)
    frame_top_music_lrc = Frame(width=200, height=40, bd=1, relief=SUNKEN)
    frame_center_music = Frame(width=400, height=250, bg='white', bd=1, relief=SUNKEN)
    frame_bottom_opeator = Frame(width=400, height=30, bg='white', bd=1, relief=SUNKEN)
    frame_center_lrc = Frame(width=200, height=280, bg='white', bd=1, relief=SUNKEN)
    frame_bottom_music_show = Frame(width=600, height=100, bg='white', bd=1, relief=SUNKEN)

    # 设置位置
    frame_top_music_category.grid(row=0, column=0)
    frame_top_music_lrc.grid(row=0, column=1)
    frame_center_music.grid(row=2, column=0)
    frame_bottom_opeator.grid(row=3, column=0)
    frame_center_lrc.grid(row=2, column=1, rowspan=2)
    frame_bottom_music_show.grid(row=4, column=0, columnspan=2)

    frame_top_music_category.grid_propagate(0)
    frame_top_music_lrc.grid_propagate(0)
    frame_center_music.grid_propagate(0)
    frame_bottom_opeator.grid_propagate(0)
    frame_center_lrc.grid_propagate(0)
    frame_bottom_music_show.grid_propagate(0)
    return  (frame_top_music_category,frame_top_music_lrc,frame_center_music,frame_bottom_opeator,frame_center_lrc,frame_bottom_music_show)

def init_frame_top_btn(frame_top_music_category,frame_top_music_lrc,frame_center_lrc):
    # 基本字体
    size_16_font = Font(size=16)

    #添加音乐栏目 中的按钮
    button = Button(frame_top_music_category,text="新歌",font=size_16_font).grid(row=0,column=0,padx=5)
    button = Button(frame_top_music_category,text="老歌",font=size_16_font).grid(row=0,column=1,padx=5)
    button = Button(frame_top_music_category,text="流行",font=size_16_font).grid(row=0,column=2,padx=5)
    button = Button(frame_top_music_category,text="经典",font=size_16_font).grid(row=0,column=3,padx=5)

    #设置歌词显示区(顶部)lable
    Label(frame_top_music_lrc,text="歌词显示区",font=size_16_font).grid(padx=50,pady=5,sticky=W+E+N+S)

    #设置歌词显示区(中央)lable
    Label(frame_center_lrc, text="暂无歌词", font=size_16_font).grid(row=0,column=0, padx=50,pady=100,sticky=W+E+N+S)


def init():
    window = Tk()
    window.title('策哥专用音乐播放器')
    init_window_size(window)

    #初始化框架
    frame_top_music_category, frame_top_music_lrc, frame_center_music, frame_bottom_opeator, frame_center_lrc, frame_bottom_music_show = init_frame()

    #初始化顶部按键或相关信息
    init_frame_top_btn(frame_top_music_category,frame_top_music_lrc,frame_center_lrc)

    # 设置显示歌曲

    music_list = Listbox(frame_center_music, height=250, width=400)

    index = 0
    for music in music_data:
        music_list.insert(index, music.get("title") +" ----- "+ music.get("author"))
        index += 1
    music_list.selection_set(0)
    music_list.grid()

    #设置底栏基本操作
    pay_all_btn = Button(frame_bottom_opeator, text="播放",command= lambda: event_obj.play_sign_song(music_list,music_data)).grid(row=0, column=0, padx=10)
    pase_song_btn = Button(frame_bottom_opeator, text="暂停播放").grid(row=0, column=1, padx=10)
    next_song_btn = Button(frame_bottom_opeator, text="下一首").grid(row=0, column=2, padx=10)
    next_page_btn = Button(frame_bottom_opeator, text="下一页").grid(row=0, column=3, padx=10)
    previous_age_btn = Button(frame_bottom_opeator, text="上一页").grid(row=0, column=4, padx=10)


    # 主事件循环
    window.mainloop()

    def pay_all_song():
        print(music_list)


if __name__ == '__main__':
    init()
